from faker import Faker
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:

    def __init__(self,base_driver=None):

        __BASE_URL = "https://work.weixin.qq.com/wework_admin/frame"

        #如果base_driver有值，直接使用
        if base_driver:
            self.driver= base_driver
        #没传driver，初始化一个driver
        else:
            self.driver = webdriver.Chrome()
            self.driver.maximize_window()
            self.driver.implicitly_wait(3)
        if not self.driver.current_url.startswith('https://'):
            #因为初始化浏览器时，不传入地址，url=data;所以可以判断地址是否是https开头，
            #如果不是，可以手动传入BASE_URL
            self.driver.get(__BASE_URL)

    def do_find(self,fun,locate=None):
        if locate:
            return self.driver.find_element(fun,locate)
        else:
            return self.driver.find_element(*fun)

    def do_finds(self,fun,locate=None):
        if locate:
            return self.driver.find_elements(fun,locate)
        else:
            return self.driver.find_elements(*fun)

    def do_quit(self):
        self.driver.quit()

    def do_wait_element_locate(self,locate):
        WebDriverWait(self.driver,10,0.5).until(expected_conditions.visibility_of_element_located(locate))
